//
//  LCGroupNewViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-18.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCGroupNewViewController.h"

@interface LCGroupNewViewController ()
@property (nonatomic) LCGroupModel *NewModel;

@end

@implementation LCGroupNewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDrawButton];
    [self creatCollection];
    [self registerNib];
    [self creatRefresh];
    self.sizeArray = [NSMutableArray array];
    [self fetchWebDataWith:kGroupHeader body:[NSString stringWithFormat:kGroupNewBody,self.page]];
}
- (void)registerNib
{
    
    self.collectionView.frame = CGRectMake(0, 0, kScreenSize.width, kScreenSize.height);
    self.flowLayout.minimumLineSpacing = 2;
    self.flowLayout.minimumInteritemSpacing = 2;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCGroupCell" bundle:nil] forCellWithReuseIdentifier:@"LCGroupCell"];
    
}
- (void)creatRefresh
{
    __weak typeof(self) weakSelf = self;
    [self.collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page = 0;
        [weakSelf fetchWebDataWith:kGroupHeader body:[NSString stringWithFormat:kGroupNewBody,weakSelf.page]];
    }];
    [self.collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page+=10;
        if (weakSelf.page>=weakSelf.NewModel.total.integerValue) {
            [MMProgressHUD show];
            [MMProgressHUD  dismissWithSuccess:@"到底了..."];
            [weakSelf.collectionView footerEndRefreshing];
            return ;
        }
        else
        {
            [weakSelf fetchWebDataWith:kGroupHeader body:[NSString stringWithFormat:kGroupNewBody,weakSelf.page]];
        }
    }];
}
- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    
    NSDictionary *dict = dataDict[@"data"];
    if (self.page==0) {
        self.NewModel = [[LCGroupModel alloc]initWithDictionary:dict error:nil];
        [self.sizeArray removeAllObjects];
        for (int i=0; i<self.NewModel.list.count; i++) {
            LCGroupListModel *model = self.NewModel.list[i];
            NSString *string = model.content;
            CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(kScreenSize.width-20, 10000.f)];
            [self.sizeArray addObject:[NSValue valueWithCGSize:size]];
        }
    }
    else
    {
        LCGroupModel *groupModel = [[LCGroupModel alloc]initWithDictionary:dict error:nil];
        [self.NewModel.list addObjectsFromArray:groupModel.list];
        for (int i=0; i<groupModel.list.count; i++) {
            LCGroupListModel *model = groupModel.list[i];
            NSString *string = model.content;
            CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(kScreenSize.width-20, 10000.f)];
            [self.sizeArray addObject:[NSValue valueWithCGSize:size]];
        }
    }
    [self.collectionView reloadData];
    
}

#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.NewModel.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGroupCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCGroupCell" forIndexPath:indexPath];
    LCGroupListModel *model = self.NewModel.list[indexPath.row];
    NSValue *value = self.sizeArray[indexPath.row];
    [cell showWithModel:model andTextSize:value.CGSizeValue];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSValue *value = self.sizeArray[indexPath.row];
     CGSize size = value.CGSizeValue;
     LCGroupListModel *model = self.NewModel.list[indexPath.row];
    if ([model.coverimg isEqualToString:@""]) {
        return CGSizeMake(kScreenSize.width-20, size.height+60);
    }
    if (size.height>60) {
        return CGSizeMake(kScreenSize.width-20,size.height+80);
    }
   
    return CGSizeMake(kScreenSize.width-20, 150);
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
    LCGroupListModel *model = self.NewModel.list[indexPath.row];
    LCGPDetailViewController *detail = [[LCGPDetailViewController alloc]init];
    detail.contentID = model.contentid;
    detail.myTitle = model.title;
    detail.content = model.content;
    [self.navigationController pushViewController:detail animated:YES];
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
    }
    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(0, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [self setDrawButton];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
